<?php
class Con_cash extends Acan{
	public $defaultAct='list';
	public $Conread = 'cash列表';
	public $Actread = array('pay'=>'充值中心','list'=>'cash列表','post'=>'插入记录','appeal'=>'申诉','call'=>'充值回调');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		$this->atitle = $this->Actread[$this->Aact];
		$this->danhao_sale_fun = Afunc_option('Acan_danhao_sale_fun')->get();
		$this->Appname = Afunc_option('Acan_danhao_sale_fun')->get();
		$this->model = Afun_db()->model(Acan_table_danhao_cash);
		$this->enum = array(
			'com'=>array('0'=>'全部','shentong'=>'申通','yuantong'=>'圆通','ems'=>'EMS','yunda'=>'韵达','shunfeng'=>'顺丰','tiantian'=>'天天','huitong'=>'汇通','zhongtong'=>'中通','guotong'=>'国通快递','quanfeng'=>'全峰','longbang'=>'龙邦','zhaijisong'=>'宅急送','nengda'=>'港中能达'),
			'from'=>array('0'=>'全部','1'=>'淘宝','2'=>'公司','3'=>'京东','4'=>'拍拍','5'=>'中移动','6'=>'当当网','7'=>'易迅'),
			'scan'=>array('1'=>'已扫描','2'=>'未扫描'),
			'scanrr'=>array('全部','是','否'),
			'price'=>array('1'=>'0.5','2'=>'1'),
			'status'=>$this->model->enum['status'],
			'cash'=>$this->model->enum['type'],
			'reason'=>array('1'=>'单号不存在','2'=>'发货地址不符','3'=>'收货地址不符','4'=>'扫描时间过早','5'=>'单号已被使用','6'=>'晚于揽收时间','7'=>'其他原因'),
			'bank_type'=>$this->model->enum['bank_type'],
		);
	}
	function Act_manage(){
		$obj = Afun_acan_new('list');
		$columnrr = $this->model->comment;//初始栏目
		$columnrr['username'] = "用户名";
		unset($columnrr['uid']);
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$key_name = $this->model->key_name;
		$columnrr = $this->model->comment;//初始栏目
		if($_GET['type']=='4') $this->model->enum['bank_type']=$this->enum['reason'];
		$uid = $this->id;
		$con->where = '1';
		if($_GET['cash_search']==1){
			$get = $_GET;
			if(!empty($get['type'])) $con->where .= ' and a.type='.$get['type'];
			if(!empty($get['status'])) $con->where .= ' and a.status='.$get['status'];
		}
		//分页函数
		if($uid>0) $con->where .= " and a.uid=$uid";
		$con->select = "a.*,b.username";
		$con->join = "LEFT JOIN `".Acan_table_user."` b ON a.uid=b.uid";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->order = 'DESC';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		include $this->Atpl();
	}
	function Act_list(){
		Afunc_user()->load_info($this->_SS['uid']);
		$area = Afun_new('exp')->area();
		$where = '';
		$sh_time = time()-86400*10;
		$reason_json = json_encode($this->enum['reason']);
		$type = 1;
		if($this->id>0 && is_numeric($this->id)){
			$where = ' and type='.$this->id;
			if($this->id==21){
				$where .=' and status=2';
			}
			$type=$this->id;
		}else{
			$where = " and type in (2,3,4,5)";
		}
		if($_GET['start_date']){
			$where .= " and a.buydate>='".$_GET['start_date']."'";
		}
		if($_GET['end_date']){
			$where .= " and a.buydate<='".$_GET['end_date']."'";
		}
		if(!$_GET['start_date']) $_GET['start_date']= Afun_time_date(0,'Y-m-d').' 00';
		if(!$_GET['end_date']) $_GET['end_date'] = Afun_time_date(Afun_time_ex(time(),1,'d'),'Y-m-d').' 00';
		$this->atitle = $this->enum['cash'][$type].'记录';
		if($this->_SS['uid']==1){
			$con->where = "1".$where;
		}else{
			$con->where = "1 and uid=".$this->_SS['uid'].$where;
		}
		$con->select = "a.*";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->order = 'DESC';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		if($type==2 || $type==4 || $type==3){
			if(count($list)>0){
				foreach ($list as $k => $v) {
					$rids[] = $v['rid'];
				}
				$emodel = Afun_db()->model(Acan_table_danhao_express);
				$explist = $emodel->findAll('id in ('.implode(',',$rids).')');
				foreach ($explist as $k => $val) {
					$edata[$val['id']] = $val;
					$edata[$val['id']]['com_alt'] = $this->enum['com'][$val['com']];
					$edata[$val['id']]['scan_alt'] = $this->enum['scan'][$val['scan']];
					$edata[$val['id']]['fh_alt'] = $area['province'][$val['f1']].$area['city'][$val['f1']][$val['f2']].$area['dist'][$val['f2']][$val['f3']];
					$edata[$val['id']]['sh_alt'] = $area['province'][$val['s1']].$area['city'][$val['s1']][$val['s2']].$area['dist'][$val['s2']][$val['s3']];
				}
			}
		}
		include $this->Atpl();
	}
	//申诉
	function Act_appeal(){
		$this->id;
		$post = $_POST;
		$rs = 0;
		if($post['id']>0){
			$rs = $this->model->update(array('type'=>4,'bank_type'=>$post['reason'],'status'=>11),array('uid'=>$this->_SS['uid'],'id'=>$post['id']));
		}
		Afun_json($rs);
	}
	//申诉审核
	function Act_appeal_do(){
		$id = $this->id;
		$status = $_GET['status'];
		if($id>0){
			$rs = $this->model->update(array('status'=>$status),array('id'=>$id));
			if($status==12){
				$cash = $this->model->findPk($id);
				if($cash) Afun_new("cash")->cash_add($cash['uid'],abs($cash['cash']),'cash',12,$this->get_cash_id());
			}
		}
		Afun_refresh('更新成功','referrer');
	}
	//插入cash
	function Act_post(){
		$arr = $_POST;
		if(empty($arr['cash_id']) || !$_POST){
			Afun_refresh('非法请求',$this->Amodr.'-pay');exit;
		}
		$save['cash_id'] = $arr['cash_id'];
		$save['cash'] = $arr['cash'];
		$save['bank_type'] = $arr['bank_type'];
		$save['uid'] = $this->_SS['uid'];
		$save['type'] = '21';
		$save['update'] = Afun_get_time();
		$save['status'] = 1;
		if($this->model->find(array('cash_id'=>$save['cash_id']))){
			$rs = $this->model->update($save,array('cash_id'=>$save['cash_id']));
		}else{
			$rs = $this->model->save($save);
		}
		Afun_json(array('rs'=>$rs,'hash'=>hash('sha1',$arr['cash_id'].$arr['cash'].$arr['bank_type'].'3jhj')));
	}
	//充值页面
	function Act_pay(){
		$mch_vno = $this->get_cash_id();
		Afunc_user()->load_info($this->_SS['uid']);
		include $this->Atpl();
	}
	//删除
	function Act_del(){
		if($this->model->deletePk($this->id)){
			Afun_refresh('删除成功','referrer');
		}
	}
	//赠送
	function Act_free(){
		
	}
	//充值回调cash
	function Act_call(){
		$arr = $_GET;
		if($arr['hash']!=hash('sha1',$arr['cash_id'].'3jhj')){
			Afun_refresh('error hash!',$this->Aapp.'-cash-pay');exit;
		}
		$save['update'] = Afun_get_time();
		$save['rid']=$arr['pay_id'];
		$save['status'] = 2;
		$cash = $this->model->find(array('cash_id'=>$arr['cash_id'],'status'=>1));
		if(!$cash){
			Afun_refresh('no cash!',$this->Aapp.'-cash-pay');exit;
		}
		$crs = Afun_new("cash")->cash_add($cash['uid'],$cash['cash'],'cash',0,$this->get_cash_id());
		if($crs!=1){
			$save['status'] = 7;
		}
		$group = Afunc_group()->getgroup($cash['uid']);
		//首次充100赠送100
		/*if(($cash['cash']*100)==10000){// && time()<strtotime('2013-10-17')){
			$data = $this->model->find(array('uid'=>$cash['uid'],'type'=>21,'status'=>2));
			if(!$data){*/
		//	}
		//}
		//包天会员 充50送1150
		if($group==14){
			$frs = Afun_new("cash")->cash_add($cash['uid'],$cash['cash']*23,'free',23,$this->get_cash_id(),'',1,'d');
			if($frs!=1){
				$save['status'] = 8;
			}
		}else{//普通会员 充多少送多少
			$frs = Afun_new("cash")->cash_add($cash['uid'],$cash['cash'],'free',23,$this->get_cash_id());
			if($frs!=1){
				$save['status'] = 8;
			}
		}
		$this->model->update($save,array('cash_id'=>$arr['cash_id'],'status'=>1));
		Afunc_user()->load_info($cash['uid']);
		Afun_refresh('充值成功',$this->Aapp.'-cash-pay');
	}
	function get_cash_id(){
		$curDateTime = date("YmdHis");
		$strDate = date("Ymd");
		$strTime = date("His");
		//4位随机数
		$randNum = rand(1000, 9999);
		//10位序列号,可以自行调整。
		$strReq = $strTime . $randNum;
		/* 商家的定单号 */
		$mch_vno = $curDateTime . $randNum;
		return $mch_vno;
	}
}

?>